package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.redis.IUserRedisService;
import cn.wolfcode.luowowo.repository.UserInfoRepository;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.Consts;
import cn.wolfcode.luowowo.util.RegistUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoRepository repository;
    @Override
    public UserInfo get(String id) {
        Optional<UserInfo> optional = repository.findById(id);
        //如果数据库不是空的
        if (optional.isPresent()){
            return optional.get();
        }
        return null;
    }


    /**
     * 检查手机号是否存在
     * true-null:不存在   false:存在
     * @param phone
     * @return
     */
    @Override
    public Boolean findByPhone(String phone) {
        UserInfo byPhone = repository.findByPhone(phone);
        return byPhone == null;
    }

    //用户账号的注册
    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //1.验证参数不能为空
        RegistUtil.isEmpty(phone,"电话不能为空");
        RegistUtil.isEmpty(nickname,"昵称不能为空");
        RegistUtil.isEmpty(password,"密码不能为空");
        RegistUtil.isEmpty(rpassword,"确认密码不能为空");
        RegistUtil.isEmpty(verifyCode,"短信验证码不能为空");
        //2.两次密码一样
        RegistUtil.isEqual(password,rpassword,"两次密码不一样");
        //3.手机号码格式是否正确
        if(!RegistUtil.isMobile(phone)){
            throw new LogicException("请输入正确的电话号码");
        }
        //4.手机号码是否已经注册在案
        if (!this.findByPhone(phone)){
            throw new LogicException("该手机号已经被注册");
        }
        //5.判断前端输入短信验证码是否正确
        String code = userRedisService.getCode(phone);
        if (code == null || !code.equalsIgnoreCase(verifyCode)){
            throw new LogicException("验证码错误");
        }
        //6.用户注册
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setEmail("");
        userInfo.setPassword(password);
        userInfo.setGender(UserInfo.GENDER_SECRET);
        userInfo.setLevel(0);
        userInfo.setCity("");
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setInfo("");
        userInfo.setState(UserInfo.STATE_NORMAL);
        userInfo.setId(null);
        repository.insert(userInfo);
    }

    @Autowired
    private IUserRedisService userRedisService;

    //手机短信验证码发送和储存
    @Override
    public void sendVerifyCode(String phone,String url,String appkey) {
        //1.验证码的生成
        String code =  UUID.randomUUID().toString().replaceAll("-","").substring(0,4);
        //2.拼接一个短信
        StringBuilder sb = new StringBuilder();
        sb.append("您好,您的注册验证码是:").append(code).append(",请在").append(Consts.VERIFY_CODE_VAI_TIME)
                .append("分钟内使用");

        //**发送短信之前限制在一定时间内短信发送次数----拓展
        userRedisService.sendCodeTimes(phone);

        //3.发送短信
        /*System.out.println(sb.toString());
        RestTemplate restTemplate = new RestTemplate();
        Map map = restTemplate.getForObject(url, Map.class, phone, sb.toString(), appkey);
        System.out.println("map---->"+map);
        if (!map.get("code").equals("10000")){
            throw new LogicException("短信发送失败");
        }*/
        //4.将验证码储存到redis中
        userRedisService.setCode(phone,code);
    }

    @Override
    public UserInfo checkLogin(String phone, String password) {
        UserInfo userInfo = repository.findByPhoneAndPassword(phone, password);
        if (userInfo == null){
            throw new LogicException("账号或密码错误");
        }
        userInfo.setPassword(null);
        return userInfo;
    }

    @Override
    public List<UserInfo> list() {
        return repository.findAll();
    }

    @Override
    public List<UserInfo> getByCity(String keyword) {
        return repository.findByCity(keyword);
    }
}
